package com.tv.nextin;

import java.util.ArrayList;
import java.util.Locale;

import com.tv.nextin.Utils.KeyboardHelper;
import com.tv.nextin.Utils.Utils;
import com.tv.nextin.adapter.CategoryAdapter;
import com.tv.nextin.bean.Category;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnTouchListener{
	
	TextView title;
	EditText edtSearch;
	ArrayList<Category> categories = new ArrayList<Category>();
	ListView listCategory;
	CategoryAdapter adapter;
	ImageView btnClear;
	
	ViewGroup main;
	
	ImageButton btnMore;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		title = (TextView) findViewById(R.id.title);
		edtSearch = (EditText) findViewById(R.id.edt_search);
		btnClear = (ImageView) findViewById(R.id.btn_clears);
		btnMore = (ImageButton) findViewById(R.id.btn_more);
		
		btnMore.setVisibility(View.VISIBLE);
		
		main = (ViewGroup) findViewById(R.id.main);
		KeyboardHelper.setupUI(MainActivity.this, main);
		dataDemo();
		
		listCategory = (ListView) findViewById(R.id.list_category);
		
		
		
		adapter = new CategoryAdapter(MainActivity.this, categories);

		listCategory.setAdapter(adapter);
		init();
//		setFont();
		search();
		gotoItem();
	}
	
	private void gotoItem(){
		listCategory.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				Intent t = new Intent(MainActivity.this, BussinessListActivity.class);
				t.putExtra("category", categories.get(position));
				startActivity(t);
				overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
			}
		});
	}
	
	private void search() {
		edtSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.toString().length() > 0){
					btnClear.setVisibility(View.VISIBLE);
				} else {
					btnClear.setVisibility(View.GONE);
				}
				
				MainActivity.this.adapter.getFilter().filter(s);
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
	}
	
	
	private ArrayList<Category> dataDemo(){
		categories.add(new Category("DENTIST"));
		categories.add(new Category("MASSAGE"));
		categories.add(new Category("FOOTBALL"));
		categories.add(new Category("SPORT"));
		categories.add(new Category("TOIEC"));
		categories.add(new Category("CUT HAIR"));
		categories.add(new Category("GYM"));
		categories.add(new Category("SWIMMING"));
		categories.add(new Category("FOOD"));
		categories.add(new Category("TRAINNING"));
		categories.add(new Category("CASTING"));
		categories.add(new Category("SUPPORT"));
		categories.add(new Category("TRANSFORMER"));
		categories.add(new Category("TAKE CARE"));
		categories.add(new Category("GO FISHING"));
		categories.add(new Category("IRONNING"));
		categories.add(new Category("FLYING"));
		
		return categories;
	}
	
	private void init(){
		title.setText(R.string.category);
		btnMore.setOnTouchListener(this);
	}
	
	public void setFont(){
		title.setTypeface(Utils.getTypefaceLight(MainActivity.this));
		edtSearch.setTypeface(Utils.getTypefaceLight(MainActivity.this));
		
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_more:
			if (event.getAction() == MotionEvent.ACTION_UP){
				Toast.makeText(MainActivity.this, getString(R.string.howisfunction), Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
		
		return false;
	}
}
